package org.bot.server.socket.impl;

import org.bot.server.job.JobEntity;
import org.bot.server.job.QuartzJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @author 沧月
 * @create 0022 2023/9/22 11:41
 * @description <webSocket 服务注册>
 */

@Configuration
@EnableWebSocket
@Component
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    ServerWebSocket serverWebSocketController;

    @Autowired
    private QuartzJob quartzJob;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(serverWebSocketController, "/clientSocket")
                .addInterceptors(new ServerWebSocketVerify())
                .setAllowedOrigins("*");

        final JobEntity jobEntity = new JobEntity();
        jobEntity.setJobClass(HeartbeatMonitor.class);
        jobEntity.setJobGroup("socket");
        jobEntity.setJobKey("socket");
        jobEntity.setTriggerGroup("heartbeat");
        jobEntity.setTriggerKey("heartbeat");
        jobEntity.setCron("0 */1 * * * ?");
        quartzJob.addJob(jobEntity);
    }
}
